import { defineStore } from "pinia";
import {ref} from "vue";
import {UserLoginInfo} from "../type/user";
import {getUser} from "../service/user.ts";
import ACCESS_ENUM from "../access/accessEnum.ts";


export const userStore = defineStore("loginUser", () => {
  const loginUser = ref<UserLoginInfo | undefined>({
    id:0,
    userName:"",
    userPhone:"",
    userAvatar:"",
    userRole:""
  })
  const updateLoginUser = async () => {
    // 从远程请求获取登录信息
    const res = await getUser();
    if (res.code === 0) {
      // @ts-ignore
      loginUser.value = res.data;
      console.log("登录用户请求成功")
      console.log(loginUser.value)
    } else {
      //合并两个对象
      // @ts-ignore
      loginUser.value = {...loginUser.value,userRole: ACCESS_ENUM.NOT_LOGIN };
    }
  };

  return { loginUser, updateLoginUser };
});
